#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Util.hpp"

using namespace std;

const string sep = "\r\n";
const string default_root = "./wwwroot";
const string home_page = "index.html";//默认的首页
const string html_404 = "wwwroot/404.html";

class HttpRequest
{
public:
    HttpRequest() {};
    ~HttpRequest() {};
    void parse()
    {
        // 1.从inbuffer中拿到第一行，分割符\r\n
        std::string line = Util::getOneLine(inbuffer, sep);
        if (line.empty())
            return;
        // 2.从请求行中提取三个字符
        cout << "line:\r\n" << line << endl;
        stringstream ss(line);
        ss >> method >> url >> httpversion;

        //3.添加web默认路径
        path = default_root;//./wwwroot
        path += url;//./wwwrot/a/b/c.html  如果url是/根目录   ./wwwroot/
        if(path[path.size()-1] == '/') path += home_page;

        // 4. 获取path对应的资源后缀
        // ./wwwroot/index.html
        // ./wwwroot/test/a.html
        // ./wwwroot/image/1.jpg
        auto pos = path.rfind(".");
        if(pos == std::string::npos) suffix = ".html";
        else suffix = path.substr(pos);

        //5.得到资源大小
        // struct stat st;
        // int n = stat(path.c_str(),&st);
        // if(n == 0) size = st.st_size;
        // else size = -1;
    }

public:
    string inbuffer;
    // string reqline;//- 首行: [方法] + [url] + [版本]
    // vector<string> reqheader;//Header: 请求的属性, 冒号分割的键值对;每组属性之间使用\r\n分隔;遇到空行表示Header部分结束
    // string body;//- Body: 空行后面的内容都是Body. Body允许为空字符串. 如果Body存在, 则在Header中会有一个Content-Length属性来标识Body的长度;

    string method;      //[方法]
    string url;         //[url]
    string httpversion; //[版本]
    string path;
    string suffix;      //.后缀
    // int  size;       //资源大小
};

class HttpResponse
{
public:
    string outbuffer;

    // string respline;
    // string respheader;
    // string respblan;
    // string body;
};